define([
	'../libs/zepto',
	'../common/base',
	'../common/common_click',
	'../common/dialog',
	'../common/yingSoftPrg',
	'../common/yingSoftCookie',
	'../common/ajax'
], function() {
	var guid = G_Cookie.getGuid();
	var BookID = G_Prg.getCookie('BOOKID');
	var winH = $(window).height();
	if(_getClientType() == 'web_iPhone') {} else {
//		$('.header').after('<a href="#" class="bendi">本地视频</a>');
		$('.bendi').unbind('click').click(function() {
			window.video.jumpToVideoList('a');
		})
	}
	$('.middle_1').css('height', winH - $('#back').height())
	getChapterMenu(guid, BookID);

	function getChapterMenu(guid, BookID) {
		var url = '/api/knowledge/getChapterMenu/?BookID=' + BookID + '&guid=' + guid;
		G_AjaxApiVideo_OSS.get(url, false, function(json) {
			var jsonData = JSON.parse(json);
			if(jsonData.status === 200) {
				var data = jsonData.data;
				var chapterMenu = data.chapterMenu.Childs;
				for(var i = 0; i < chapterMenu.length; i++) {
					var liList = '<li class="buy" ChapterID=' + chapterMenu[i].ChapterID + ' IsBuy=' + chapterMenu[i].IsBuy + ' IsFree=' + chapterMenu[i].IsFree + ' KnowNums=' + chapterMenu[i].KnowNums + ' OnlyCode=' + chapterMenu[i].OnlyCode + '><a href="#">' + chapterMenu[i].ChapterName + '</a><span>购买</span></li>'
					$('.soft').append(liList);
					$('.soft li').each(function() {
						if($(this).attr('IsFree') == 1) {
							$(this).attr('class', 'free').find('span').text('免费体验');
						} else if($(this).attr('IsBuy') == 1) {
							$(this).attr('class', 'free').find('span').text('已购买');
						}
					})
					$('.soft li').click(function() {
						var IsFree = $(this).attr('IsFree');
						var IsBuy = $(this).attr('IsBuy');
						var ChapterID = $(this).attr('ChapterID');
						var OnlyCode = $(this).attr('OnlyCode');
						var chapterName = $(this).find('a').text();
						var BookID = G_Prg.getCookie('BOOKID');
						if(IsFree == 1 || IsBuy == 1) {
							window.location.href = '/Five?ChapterID=' + ChapterID + '&chapterName=' + chapterName;
						} else if(IsFree == 0) {
							if(_getClientType() == "web_android") {
								var OnlyCode = $(this).attr('OnlyCode');
								window.location.href = '/buyVideo?ChapterID=' + ChapterID + '&OnlyCode=' + OnlyCode + '&BookID=' + BookID + '&chapterName=' + escape(chapterName);
							} else if(_getClientType() == "web_iphone" || "ipad" || "iphone") {
								var userID_str = G_Cookie.getUserID(); //变量存储用户ID;—>
								var AppEName_str = G_Cookie.getAppEName(); //变量存储当前将要购买的科目名称(需转换utf8)
								var AppName_str = encodeURI(G_Cookie.getAppName()); // 字符串转换utf-8
								var SubjectName_str = chapterName//名称考点的名称(如:清热剂)
								var SubjectName_str = encodeURI(SubjectName_str); // 字符串转换utf-8
								var Guid_str = G_Cookie.getGuid(); //引导码
								var BookID = BookID; // 考点名称
								var CptID = ChapterID; //考点所属章节名称
								parent.window.location.href = "iosWeiKePay://" + "@@" + userID_str + "@@" + AppName_str + "@@" + SubjectName_str + "@@" + Guid_str + "@@" + BookID + "@@" + CptID;
								console.log(userID_str + "@@" + AppName_str + "@@" + SubjectName_str + "@@" + Guid_str + "@@" + BookID + "@@" + CptID)
							}
						}
					})
				}
				myscroll.refresh();
			} else {
				alert('知识点章节数据为空');
			}
		});
	}
});

function _getClientType() {
	var browser = {
		versions: function() {
			var u = navigator.userAgent,
				app = navigator.appVersion;
			return {
				ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
				android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1,
				iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1,
				iPad: u.indexOf('iPad') > -1,
				webApp: u.indexOf('Safari') == -1
			};
		}()
	};
	if(browser.versions.iPhone || browser.versions.iPad) {
		return 'web_iPhone';
	}
	if(browser.versions.ios || browser.versions.android || browser.versions.webApp) {
		return 'web_android';
	}
	return 'web_pc';
}